<template>
  <div class="tools_box">
    <!-- 弹窗页面 -->
    <el-dialog :close-on-click-modal='false' v-drag :title="$t(type_info)" v-if="dialogFormVisible" :visible.sync="dialogFormVisible" :width="type_info === '查看' ? '1100px':'900px'" :top="type_info === '查看' ?'10vh':'5vh'" @close="close">
      <div :class="type_info === '查看' ? 'VirtualPageFormShow':'VirtualPageForm'">
        <!-- 查看页面 -->
        <VirtualShow ref="child_show_form" v-if="type_info === '查看'" v-bind:edit_form="form" v-bind:type_info="type_info" @childForm="handleChildForm($event)"></VirtualShow>
        <!-- 添加/编辑页面 -->
        <VirtualEdit ref="child_form" v-if="['添加','编辑'].includes(type_info)" v-bind:edit_form="form" v-bind:type_info="type_info" v-bind:dialogFormVisible="dialogFormVisible" @childForm="handleChildForm($event)"></VirtualEdit>
      </div>
      <!-- 弹窗控件 -->
      <div slot="footer" class="dialog-footer">
        <el-button id="submit-btn" type="primary" @click="getChildData" v-if="['添加','编辑'].includes(type_info)" class="addSubmit">{{$t('确 定')}}</el-button>
        <el-button id="cancel-btn" @click="close" class="addClose">{{$t('取 消')}}</el-button>
      </div>
    </el-dialog>
    <warningTools :dialogVisible="dialogVisible" :msg="msg" @visibleSubmit="visibleSubmit" />
  </div>
</template>

<script>
import VirtualEdit from './VirtualEdit'
import VirtualShow from './VirtualShow'
import { addVirtualApi,editVirtualApi } from '@/api/network/api/virtual'
import warningTools from "@/components/warningTools/index";
export default {
  name: 'VirtualTools',
   components: {
    VirtualEdit,
    VirtualShow,
    warningTools
  },
  props: [
    'edit_form',
    'type_info',
    'multipleSelection'
  ],
  data () {
    return {
      form: {},
      msg:"",
      dialogVisible:false,
      dialogFormVisible: false,
      visible: false
    }
  },
  watch: {
    edit_form: {
      handler (val) {
        this.form = val
      },
      immediate: true
    }
  },
  methods: {
    edit_info () {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['child_form'].classRef()
      })
    },
    getChildData () {
      this.$refs.child_form.send_form()
    },
    visibleSubmit() {
      this.dialogVisible = false;
      this.msg = "";
    },
    handleChildForm (params) {
      console.log('提交参数',params);
      const func = this.type_info === '添加' ? addVirtualApi : editVirtualApi;
      func(params).then(response => {
        if (response.status === 0) {
          this.$messages.success(this.$t(response.msg))
          this.dialogFormVisible = false
          this.form = {}
          this.$emit('listData')
        }
      }).catch(err => {
        console.log(err);
         if (err.status === -1000) {
          this.dialogVisible = true;
          this.msg = err.msg || "";
        }
      })
    },
    close () {
      this.dialogFormVisible = false
      if (['添加','编辑'].includes(this.type_info)) {
        this.$refs.child_form.close()
      }
    }
  }
}
</script>

<style scoped>
.tools_box {
  text-align: left;
}
.VirtualPageForm {
  max-height: calc(90vh - 144px);
  overflow-y: auto;
}
.VirtualPageFormShow {
  max-height: calc(90vh - 244px);
  overflow-y: auto;
}
</style>
